package fun.yizhierha.tools.other.controller;

import fun.yizhierha.common.annotation.Log;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.ValidUtils;
import fun.yizhierha.common.utils.*;
import fun.yizhierha.tools.other.domain.ToolQiniuContent;
import fun.yizhierha.tools.other.domain.vo.CreateToolQiniuContentVo;
import fun.yizhierha.tools.other.domain.vo.UpdateToolQiniuContentVo;
import fun.yizhierha.tools.other.domain.vo.RetrieveToolQiniuContentVo;

import fun.yizhierha.tools.other.service.ToolQiniuContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** generated by EH-Admin
* @author erha
* @date Sun Jan 08 14:05:28 CST 2023
**/
@Api(tags = "系统管理:存储管理:七牛云")
@RestController
@RequestMapping("/api/tools/storage/qiniu")
@RequiredArgsConstructor
public class ToolQiniuContentController{

    private final ToolQiniuContentService toolQiniuContentService;

    @ApiOperation("获取七牛云文件")
    @Log("获取七牛云文件")
    @GetMapping
    @PreAuthorize("@eh.check('tools:storage:qiniu:list')")
    public R<PageUtils<ToolQiniuContent>> list(RetrieveToolQiniuContentVo retrieveToolQiniuContentVo, Query.PageVo pageVo){
        PageUtils<ToolQiniuContent> res = toolQiniuContentService.list(retrieveToolQiniuContentVo,pageVo);
        return R.<PageUtils<ToolQiniuContent>>ok().setData(res);
    }

    @ApiOperation("新增七牛云文件")
    @Log("新增七牛云文件")
    @PostMapping
    @PreAuthorize("@eh.check('tools:storage:qiniu:add')")
    public R<ToolQiniuContent> add(@RequestParam MultipartFile file){
        ToolQiniuContent toolQiniuContent = toolQiniuContentService.save(file);
        return R.<ToolQiniuContent>ok().setData(toolQiniuContent);
    }

    @ApiOperation("修改七牛云文件")
    @Log("修改七牛云文件")
    @PutMapping
    @PreAuthorize("@eh.check('tools:storage:qiniu:edit')")
    public R<List<BaseErrDto>> edit(@Validated @RequestBody ValidList<UpdateToolQiniuContentVo> updateToolQiniuContentList,
                    BindingResult bindingResult){
        List<BaseErrDto> errDtoList = ValidUtils.getBaseErrDtoByBindingRes(updateToolQiniuContentList, bindingResult);
        if (errDtoList.isEmpty()) {
            toolQiniuContentService.edit(updateToolQiniuContentList,errDtoList);
            if (errDtoList.isEmpty()){
                return R.ok();
            }else {
                return  R.<List<BaseErrDto>>error(
                        BizCodeEnum.Client_Error_CRUD.getCode(),
                        BizCodeEnum.Client_Error_CRUD.getMsg()
                ).setData(errDtoList);
            }
        }
        return  R.<List<BaseErrDto>>error(
                BizCodeEnum.Client_Error_CRUD.getCode(),
                BizCodeEnum.Client_Error_CRUD.getMsg()
        ).setData(errDtoList);
    }

    @ApiOperation("删除七牛云文件")
    @Log("删除七牛云文件")
    @DeleteMapping
    @PreAuthorize("@eh.check('tools:storage:qiniu:del')")
    public R del(@RequestBody Set<Long> ids){
        Map<String, Object> errMap = toolQiniuContentService.remove(ids);
        if (errMap == null) {
            return R.ok();
        }else {
            return R.error(BizCodeEnum.Client_Error_BadRequest.getCode(), "部分/全部失败：").setData(errMap);
        }
    }

    @ApiOperation("导出数据")
    @Log("导出数据")
    @GetMapping("/download")
    @PreAuthorize("@eh.check('tools:storage:qiniu:list')")
    public void download(HttpServletResponse response){
        toolQiniuContentService.download(response);
    }

}